<?php

namespace common\jobs;

use Yii;
use yii\base\Exception;

/**
 * 交易自动过期处理
 * Class TradeExpiredJob.
 */
class TestRetryableJob extends \yii\base\BaseObject implements \yii\queue\RetryableJobInterface {

    public $content = null;
    public $time = 0;

    /**
     * @param \yii\queue\Queue $queue which pushed and is handling the job
     */
    public function execute($queue) {
        if ($this->content == null) {
            return false;
        }

        Yii::error($this->content);
        Yii::error($this->time);
        if ($this->time == 0) {
            $this->time++;
            throw new Exception('重试');
        }
        Yii::error($this->time);
        return true;
    }

    /**
     * @inheritdoc
     */
    public function getTtr() {
        return 1;
    }

    /**
     * @inheritdoc
     */
    public function canRetry($attempt, $error) {
        return $attempt < 3;
    }

}
